package com.zys.sac.core.ext;

import cn.hutool.crypto.digest.MD5;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 集成中台，加密方式与中台保持一致
 *
 * lyy
 * 2022/5/25
 */
public class MD5PasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence rawPassword) {
        if (rawPassword == null) {
            throw new IllegalArgumentException("密码不能为空");
        } else {
            return  MD5.create().digestHex(rawPassword.toString());
        }
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        if (rawPassword == null) {
            throw new IllegalArgumentException("密码不能为空");
        } else if (encodedPassword != null && encodedPassword.length() != 0){
           String rawMd5Password= MD5.create().digestHex(rawPassword.toString());
           if(rawMd5Password.equals(encodedPassword)){
               return true;
           }
        }
        return false;
    }
}
